<template>
  <div class="new-apps">
    <div class="title">New Apps</div>
    <div class="apps-list">
      <a class="app-item" :href="getDetailUrl" v-for="(item, index) in ToolsData.slice(0, 12)" :key="index" @click="handleClickApp('New Apps', item.name)">
        <img class="img-icon" :src="item.icon" />
        <div class="info">
          <div class="name">{{  item.name  }}</div>
          <div class="category">{{  item.category  }}</div>
          <div class="rating">
            {{ item.rating }}
            <div class="icon">★</div>
          </div>
        </div>
      </a>
    </div>
  </div>
</template>

<script setup>
import ToolsData from '~/public/data/apps/social.json'
import { homeParams } from '~/utils/homeParams'
import { ref, onMounted } from 'vue'

const props = defineProps({
  categories: {
    type: Array,
    required: true
  }
})

const detailUrls = ref(new Map())

const handleClickApp = (type, name) => {
  logEvent('Click_APP', {
    Subzone: type,
    App: name,
  })
}

const generateDetailUrls = () => {
  ToolsData.slice(0, 12).forEach(item => {
    const queryParams = {
      type: 'apps',
      slug: item.slug,
      ...homeParams()
    }
    detailUrls.value.set(item.slug, `/details?${new URLSearchParams(queryParams).toString()}`)
  })
}

onMounted(() => {
  generateDetailUrls()
})

const getDetailUrl = (item) => {
  return detailUrls.value.get(item.slug)
}
</script>

<style scoped lang="scss">
.new-apps {
  margin-top: 1rem;
  .title {
    @include title;
  }
  .apps-list {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 1rem;
    margin-top: 1rem;
    @media screen and (max-width: 768px) {
      grid-template-columns: 1fr;
    }
    .app-item {
      display: flex;
      gap: 1rem;
      color: #000;
      text-decoration: none;
      .img-icon {
        width: 4.5rem;
        height: 4.5rem;
        border-radius: 1rem;
        margin-bottom: 0.5rem;
        object-fit: cover;
        background-image: url('/img/default.png');
        background-size: 4.5rem 4.5rem;
        background-position: center;
      }
      .info {
        display: flex;
        flex-direction: column;
        gap: .1rem;
        .name {
          font-size: 0.9rem;
          margin-bottom: 0.25rem;
          font-weight: 500;
          overflow: hidden;
          width: 9rem;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .category {
          font-size: .7rem;
          color: #666666;
        }
        .rating {
          display: flex;
          align-items: center;
          font-size: 0.8rem;
          color: #F75867;
          .icon {
            color: #666;
            margin-left: 0.25rem;
          }
        }
      }
    }
  }
}
</style>